package com.opmsys.service.impl;

import com.opmsys.mapper.PersonMapper;
import com.opmsys.pojo.dto.*;
import com.opmsys.pojo.vo.PersonInfo;
import com.opmsys.pojo.entity.Person;
import com.opmsys.service.PersonService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class PersonServiceImpl implements PersonService {
    private final PersonMapper personMapper;

    public PersonServiceImpl(PersonMapper personMapper) {
        this.personMapper = personMapper;
    }

    /**
     * 新增员工
     * @param person
     * @return
     */
    @Override
    public Boolean add(Person person) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String name = person.getName();
        String password = person.getPassword();
        Long number = person.getNumber();
        String headImg = person.getHeadImg();
        Integer status = person.getStatus();
        String entryDate = formatter.format(date);
        return personMapper.addPerson(name,number,password,status,entryDate,headImg)== 1;
    }

    /**
     * 更改员工信息
     * @param person
     * @return
     */
    @Override
    public Boolean change(Person person) {
        Integer id = person.getId();
        String name = person.getName();
        Long number = person.getNumber();
        Integer status = person.getStatus();
        String headImg = person.getHeadImg();
        String password = person.getPassword();
        String entryDate = person.getEntryDate();
        Person p = personMapper.selectById(id);
        if (p == null){
            return personMapper.update(name,headImg,number,password,status,entryDate,id)==1;
        }
        return false;
    }


    /**
     * 登录时查询账户和密码
     * @param loginInfo
     * @return
     */
    @Override
    public Person selectPersonByNumAndPassword(LoginInfo loginInfo) {
        Long number = loginInfo.getNumber();
        String password = loginInfo.getPassword();
        return personMapper.selectPersonByNumAndPassword(number,password);
    }

//    @Override
//    public Boolean selectPersonByNum(Long number) {
//        return personMapper.selectPersonByNum(number)==null;
//    }

    /**
     * 根据账号查询是否存在
     * @param number
     * @return
     */
    @Override
    public int selectPersonByNum(Long number) {
        List<Person> personList = personMapper.selectPersonByNum(number);
        System.out.println(personList);
        return personList.size();
    }


    /**
     * 管理员更改用户密码
     * @param passwordAbout
     * @return
     */
    @Override
    public Boolean changePasswordByAdmin(PasswordAbout passwordAbout) {
        String password = passwordAbout.getRawPassword();
        Long number = passwordAbout.getNumber();
        return personMapper.changePassword(number,password)==1;
    }

    /**
     * 用户自行更改密码
     * @param passwordAbout
     * @return
     */
    @Override
    public Boolean changePasswordByUser(PasswordAbout passwordAbout) {
        String rawPassword = passwordAbout.getRawPassword();
        String newPassword = passwordAbout.getNewPassword();
        Long number = passwordAbout.getNumber();
        Boolean isRawPasswordRight = personMapper.selectPersonByNumAndPassword(number,rawPassword)!=null;
        if (isRawPasswordRight){
            return personMapper.changePassword(number,newPassword)==1;
        }else {
            return false;
        }
    }

    /**
     * 模糊查询
     * @param fuzzySearchAbout
     * @return
     */
    @Override
    public List<PersonInfo> selectPersonListByFuzzyKeyWord(FuzzySearchAbout fuzzySearchAbout) {
        String keyWords = fuzzySearchAbout.getKeyWords();
        Integer status = fuzzySearchAbout.getStatus();

        Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
        boolean isKeyWordsNum = pattern.matcher(keyWords).matches();
        List<Person> personList;
        if (isKeyWordsNum){
            personList = personMapper.fuzzySelectPersonByNumber(status,Long.parseLong(keyWords));
        }else {
            personList = personMapper.fuzzySelectPersonByName(status,keyWords);
        }

        List<PersonInfo> personInfoList = new ArrayList<>();
        personList.forEach(person -> {
            personInfoList.add(new PersonInfo(person));
        });

        return personInfoList;
    }

    /**
     * 员工模糊查询并分页
     * @param pagingSearchAbout
     * @return
     */
    @Override
    public List<PersonInfo> pagingSelectPersonListByFuzzyKeyWord(PagingSearchAboutPerson pagingSearchAbout) {
        String keyWords = pagingSearchAbout.getKeyWords();
        Integer status = pagingSearchAbout.getStatus();
        Integer pageSize = pagingSearchAbout.getPageSize();
        Integer pageNum = (pagingSearchAbout.getPage()-1)*pageSize;


        Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
        boolean isKeyWordsNum = pattern.matcher(keyWords).matches();
        List<Person> personList;
        if (isKeyWordsNum){
            personList = personMapper.pagingFuzzySelectPersonByNumber(status,Long.parseLong(keyWords),pageNum,pageSize);
        }else {
            personList = personMapper.pagingFuzzySelectPersonByName(status,keyWords,pageNum,pageSize);
        }

        List<PersonInfo> personInfoList = new ArrayList<>();
        personList.forEach(person -> {
            personInfoList.add(new PersonInfo(person));
        });

        return personInfoList;
    }

    /**
     * 根据id查询单个员工账户信息
     * @param id
     * @return
     */
    @Override
    public Person selectById(Integer id) {
        return personMapper.selectById(id);
    }

    /**
     * 根据模糊查询条件，查询总信息数
     * @param pagingSearchAbout
     * @return
     */
    @Override
    public int pageTotal(PagingSearchAboutPerson pagingSearchAbout) {
        String keyWords = pagingSearchAbout.getKeyWords();
        Integer status = pagingSearchAbout.getStatus();
        Integer pageSize = pagingSearchAbout.getPageSize();
        Integer pageNum = (pagingSearchAbout.getPage()-1)*pageSize;


        Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
        boolean isKeyWordsNum = pattern.matcher(keyWords).matches();
        List<Person> personList;
        if (isKeyWordsNum){
            personList = personMapper.pagingFuzzySelectPersonByNumberTotal(status,Long.parseLong(keyWords),pageNum,pageSize);
        }else {
            personList = personMapper.pagingFuzzySelectPersonByNameTotal(status, keyWords, pageNum, pageSize);
        }

        return personList.size();
    }


}
